package org.meeting.radish.controller;

import lombok.extern.slf4j.Slf4j;
import org.meeting.radish.entity.AnswerKey;
import org.meeting.radish.entity.Question;
import org.meeting.radish.service.AnswerKeyService;
import org.meeting.radish.service.QuestionService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/answers")
public class AnswerKeyController {

    @Resource
    AnswerKeyService answerKeyService;

    @Resource
    QuestionService questionService;

    @GetMapping("/question/{qid}")
    public String questionAllAnswerKey(Map<String, Object> map, @PathVariable("qid") Integer qid) {
        List<AnswerKey> questionAnswers = answerKeyService.queryAllAnswerKeyByQid(qid);
        map.put("questionAnswers", questionAnswers);
        map.put("qid", qid);
        return "answerKeys";
    }

    @GetMapping("/key/{uid}/{qid}")
    public String oneAnswerKey(@PathVariable("uid") Integer uid, @PathVariable("qid") Integer qid, Map<String, Object> map) {
        AnswerKey answerKey = answerKeyService.queryAnswerKeyByUidQid(uid, qid);
        map.put("answerKey", answerKey);
        log.info("{}", answerKey);
        return "ansKey";
    }

    @GetMapping("/delete/{uid}/{qid}")
    public String deleteAnswerKey(AnswerKey answerKey) {
        log.info("{}", answerKey);
        answerKeyService.deleteAnswerKey(answerKey);
        return "redirect:/";
    }

    @GetMapping("/add/{qid}")
    public String addAnswerKey(@PathVariable Integer qid, @CookieValue("uid") Integer uid, Map<String, Object> map) {
        Question question = questionService.queryQuestionByQid(qid);
        map.put("question", question);
        map.put("uid", uid);
        return "addAnsKey";
    }

    @GetMapping("/update/{qid}")
    public String updateAnswerKey(@PathVariable Integer qid, @CookieValue("uid") Integer uid , Map<String, Object> map) {
        AnswerKey answerKey = answerKeyService.queryAnswerKeyByUidQid(uid, qid);
        map.put("answerKey", answerKey);
        map.put("uid", uid);
        return "updateAnsKey";
    }


    @PostMapping("/submit/{qid}")
    public String addAnswerKey(@PathVariable("qid") Integer qid, AnswerKey answerKey) {
        answerKeyService.changeAnswerKey(answerKey);
        return "redirect:/answers/update/" + qid;
    }

}
